package com.SMP.controller.class_room;


import com.SMP.model.Msg;
import com.SMP.model.People;
import com.SMP.service.BroadcastService;
import com.SMP.service.PeopleService;
import com.SMP.utils.Face;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class PeopleController {

    @Autowired
    PeopleService peopleService;

    @Autowired
    BroadcastService broadcastService;

    /**
     * @Description: 增加人员
     * @Param: [people]
     * @return: com.SMP.model.Msg
     * @Author: WYHY
     * @Date: 2018/12/28
     */
    @RequestMapping(value = "/addPeople", method = RequestMethod.POST)
    @ResponseBody
    public Msg addPeople(@ModelAttribute("people") People people,@RequestParam("file") MultipartFile file) {
        broadcastService.uploadFile(file,"people/");
        int result = peopleService.addPeople(people,file);
        if (result > 0) {
            return Msg.success();
        } else {
            return Msg.fail();
        }
    }

    /**
     * @Description: 删除人员
     * @Param: [id]
     * @return: com.SMP.model.Msg
     * @Author: WYHY
     * @Date: 2018/12/28
     */
    @RequestMapping(value = "/delPeople", method = RequestMethod.DELETE)
    @ResponseBody
    public Msg delPeople(@RequestParam(value = "id", defaultValue = "no") String id) {
        People people = new People();
        people.setId(id);
        people.setStandby1("2");
        int result = peopleService.updPeople(people);
        if (result > 0) {
            return Msg.success();
        } else {
            return Msg.fail();
        }
    }

    /**
     * @Description: 修改人员信息
     * @Param: [people]
     * @return: com.SMP.model.Msg
     * @Author: WYHY
     * @Date: 2018/12/28
     */
    @RequestMapping(value = "/updPeople", method = RequestMethod.POST)
    @ResponseBody
    public Msg updPeople(@ModelAttribute("people") People people) {
        int result = peopleService.updPeople(people);
        if (result > 0) {
            return Msg.success();
        } else {
            return Msg.fail();
        }
    }

    /**
     * @Description: 条件查询人员信息
     * @Param: [people]
     * @return: com.SMP.model.Msg
     * @Author: WYHY
     * @Date: 2018/12/28
     */
    @RequestMapping(value = "/selPeoples", method = RequestMethod.POST)
    @ResponseBody
    public Msg selPeoples(@ModelAttribute("people") People people,@RequestParam(value = "pn")Integer pn,@RequestParam(value = "ps")Integer ps) {
//        PageInfo pageInfo
        PageHelper.startPage(pn, ps);
        List<People> list = peopleService.selPeoples(people);
        PageInfo<People> result = new PageInfo<People>(list,  5);
        if (result.getList().size() > 0) {
            return Msg.success().add("people", result);
        } else {
            return Msg.fail();
        }
    }

    /**
    * @Description: 主键查询人员信息
    * @Param: [id]
    * @return: com.SMP.model.Msg
    * @Author: WYHY
    * @Date: 2018/12/28
    */
    @RequestMapping(value = "/selByPrimaryKey", method = RequestMethod.POST)
    @ResponseBody
    public Msg selByPrimaryKey(@RequestParam(value = "id", defaultValue = "no") String id) {
        People result = peopleService.selByPrimaryKey(id);
        if (result != null) {
            return Msg.success().add("people", result);
        } else {
            return Msg.fail();
        }
    }
}
